<?php
/**
 * 地方车展-配套费用模型
 * @author jianhui
 *
 */
class carMatchingModel extends MY_Model{
    function carMatchingModel(){
        parent::MY_Model();
        $this->table = 'CarMatching';
    }
    /**
     * 更新父类金额(non-PHPdoc)
     * @see MY_Model::updateParentMoneys()
     */
    function updateParentMoneys($parentId){
        $this->CI->load->model('carProjectModel');
        $this->CI->carProjectModel->updateMoney($parentId);
    }
    /**
     * 获取父级关联ID(non-PHPdoc)
     * @see MY_Model::getParentId()
     */
    function getParentId($data){
        return isset($data['projectId']) && $data['projectId'] ? (int)$data['projectId'] : 0;
    }
    /**
     * 包装数据(non-PHPdoc)
     * @see MY_Model::packing()
     */
    function packing($list){
        $fileIds = array();
        foreach ($list as $v){
            if($v['fileId'] && !in_array($v['fileId'], $fileIds)){
                $fileIds[] = (int)$v['fileId'];
            }
        }
        //附件数据
        $this->CI->load->model('fileModel');
        $fileList = $this->CI->fileModel->getList(array('id'=>array('in',$fileIds)));
        //明细类型
        $detailType = $this->config->item('offlineDetailType');
        //费用项目描述
        $descType = $this->config->item('matchingDetailDescType');
        //角色列表
        $roleList = $this->config->item('roleList');
        //审核结果
        $auditRs = $this->config->item('auditRs');
        foreach ($list as $k => $v){
            $list[$k]['typeStr'] = isset($detailType[$v['type']]) ? $detailType[$v['type']] : '';
            $list[$k]['descTypeStr'] = isset($descType[$v['type']][$v['descType']]) ? $descType[$v['type']][$v['descType']] : '';
            if ($v['descType'] == -1){
                $list[$k]['descTypeStr'] .= '-'.$v['descTypeAlias'];
            }
            //预算审核状态
            $list[$k]['budgetAuditStr'] = isset($roleList[$v['budgetAuditRole']]) ? $roleList[$v['budgetAuditRole']].'已审核' : '';
            if ($list[$k]['budgetAuditStr']){
                $list[$k]['budgetAuditStr'] .= isset($auditRs[$v['budgetAuditRs']]) ? ','.$auditRs[$v['budgetAuditRs']] : '';
            }else{
                $list[$k]['budgetAuditStr'] = isset($auditRs[$v['budgetAuditRs']]) ? $auditRs[$v['budgetAuditRs']] : '';
            }
            //结算审核状态
            $list[$k]['settleAuditStr'] = isset($roleList[$v['settleAuditRole']]) ? $roleList[$v['settleAuditRole']].'已审核' : '';
            if ($list[$k]['settleAuditStr']){
                $list[$k]['settleAuditStr'] .= isset($auditRs[$v['settleAuditRs']]) ? ','.$auditRs[$v['settleAuditRs']] : '';
            }else{
                $list[$k]['settleAuditStr'] = isset($auditRs[$v['settleAuditRs']]) ? $auditRs[$v['settleAuditRs']] : '';
            }
            //附件数据
            $list[$k]['fileData'] = isset($fileList[$v['fileId']]) ? $fileList[$v['fileId']] : array();
        }
        return $list;
    }
    /**
     * 更新金额
     * @param number $id
     */
    function updateMoney($id = 0){
        //广宣数据
        $matchingData = $this->getData($id);
    
        $data  = array('createTime'=>$matchingData['createTime']+1,'isSync'=>0);
        //预算审核金额
        in_array($matchingData['budgetAuditRs'], array(1,4)) && $data['budgetAudit'] = $matchingData['budgetApply'];
        //结算审核金额
        in_array($matchingData['settleAuditRs'], array(1,4)) && $data['settleAudit'] = $matchingData['settleApply'];
        
        $this->edit($id, $data);
    }
    /**
     * 统计对应父级ID的金额
     * @param int $projectId
     * @return array
     */
    function getMoney($projectId){
        $data = array(
            'subsidyMatching' => 0,//配套补贴
            'budgetApply' => 0,
            'budgetAudit' => 0,
            'settleApply' => 0,
            'settleAudit' => 0,
        );
        $findList = $this->getList(array('projectId'=>$projectId));
        foreach ($findList as $v){
            $data['subsidyMatching'] += $v['subsidyMoney'];
            $data['budgetApply'] += $v['budgetApply'];
            in_array($v['budgetAuditRs'], array(1,4)) && $data['budgetAudit'] += $v['budgetAudit'];
            if ($v['isSettle'] == 1){
                $data['settleApply'] += $v['settleApply'];
                in_array($v['settleAuditRs'], array(1,4)) && $data['settleAudit'] += $v['settleAudit'];
            }
        }
        return $data;
    }
    /**
     * 预算审核状态统计
     * @param int $projectId
     * @return multitype:
     */
    function getBudgetStatusCount($projectId) {
        $findList = $this->getList(array('projectId'=>$projectId,'budgetApply > '=>0), null, null, 'budgetAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['budgetAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 广宣通过项目ID一键预算审核
     * @param int $projectId
     * @param number $audit
     * @param array $user
     */
    function budgetAuditByProjectId($projectId, $audit = 1,$user = array()){
        $findList = $this->getList(array('projectId'=>$projectId));
        foreach ($findList as $v){
            $auditData = array(
                'budgetAudit' => $audit==1 ? $v['budgetApply'] : 0,
                'budgetAuditRs' => $audit,
                'budgetAuditRole' => $user['userRole']
            );
            $this->db->update($this->table, $auditData, array('id'=>$v['id']));
        }
    }
    /**
     * 广宣单个预算审核
     * @param number $id
     * @param number $audit
     * @param array $data
     * @return object
     */
    function budgetAudit($id, $audit = 1, $user = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'budgetAudit' =>  $audit==1 ? $findData['budgetApply'] : 0,
            'budgetAuditRs' => $audit,
            'budgetAuditRole' => $user['userRole']
        );
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
    /**
     * 结算审核状态统计
     * @param int $projectId
     * @return multitype:
     */
    function getSettleStatusCount($projectId) {
        $findList = $this->getList(array('projectId'=>$projectId,'isSettle'=>1,'settleApply > '=>0), null, null, 'settleAuditRs');
        $count = array();
        foreach ($findList as $v){
            $count[$v['settleAuditRs']] ++;
        }
        return $count;
    }
    /**
     * 广宣通过项目ID一键结算审核
     * @param int $projectId
     * @param number $audit
     * @param array $user
     */
    function settleAuditByProjectId($projectId, $audit = 1,$user = array()){
        $findList = $this->getList(array('projectId'=>$projectId,'isSettle'=>1));
        foreach ($findList as $v){
            $auditData = array(
                'settleAudit' => $audit==1 ? $v['settleApply'] : 0,
                'settleAuditRs' => $audit,
                'settleAuditRole' => $user['userRole']
            );
            $this->db->update($this->table, $auditData, array('id'=>$v['id']));
        }
    }
    /**
     * 结算单个审核
     * @param number $id
     * @param number $audit
     * @param array $user
     * @return object
     */
    function settleAudit($id, $audit = 1, $user = array()){
        $findData = $this->getData($id);
        $auditData = array(
            'settleAudit' =>  $audit==1 ? $findData['settleApply'] : 0,
            'settleAuditRs' => $audit,
            'settleAuditRole' => $user['userRole']
        );
        return $this->db->update($this->table, $auditData,array('id'=>$id));
    }
}